let app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showModal: false,
    modelname: '',
    name: '',
    beginweek: '',
    endweek: '',
    termid: ''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let current_time = Date.parse(new Date());
    var date = new Date(current_time);
    var Y = date.getFullYear();
    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1);
    var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();

    this.setData({
      beginweek: Y + '-' + M + '-' + D,
      endweek: Y + '-' + M + '-' + D
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this;
    that.getTermList();

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  addTerm: function (e) {
    let that = this;

    that.setData({
      showModal: true,
      modalname: e.currentTarget.dataset.type
    })
  },

  modifyTerm: function (e) {
    let that = this;

    that.setData({
      showModal: true,
      termid: e.currentTarget.dataset.termid,
      modalname: e.currentTarget.dataset.type,
      name: e.currentTarget.dataset.name,
      beginweek: e.currentTarget.dataset.beginweek,
      endweek: e.currentTarget.dataset.endweek
    })
  },

  preventTouchMove: function () { },
  /**
   * 隐藏模态对话框
   */
  hideModal: function () {
    this.setData({
      showModal: false
    });
  },
  /**
   * 对话框取消按钮点击事件
   */
  onCancel: function () {
    let that = this;
    that.hideModal();
    this.setData({
      name: ''
    });
  },
  /**
   * 对话框确认按钮点击事件
   */
  onConfirm: function () {
    var that = this;
    console.log(app.globalData)

    if (that.data.modalname == '添加学期') {
      //添加学期信息
      wx.request({
        url: app.globalData.url + '/createTerm',
        method: 'POST',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          userId: app.globalData.userInfo.id,
          name: that.data.name,
          beginweek: that.data.beginWeek,
          endweek: that.data.endWeek
        },
        success: res => {
          wx.showToast({
            title: '创建成功',
            icon: 'success',
            duration: 3000
          });
          that.setData({
            showModal: false
          })
          that.getTermList();
        },
        fail: err => {
          wx.showToast({
            title: '失败',
            icon: 'error',
            duration: 3000
          });
        }
      });
    } else {
      //修改学期信息
      wx.request({
        url: app.globalData.url + '/updateTerm',
        method: 'PUT',
        header: {
          'content-type': 'application/x-www-form-urlencoded'
        },
        data: {
          id: that.data.termid,
          name: that.data.name,
          beginweek: that.data.beginweek,
          endweek: that.data.endweek
        },
        success: res => {
          wx.showToast({
            title: '修改成功',
            icon: 'success',
            duration: 3000
          });
          that.setData({
            showModal: false
          })
          that.getTermList();
        },
        fail: err => {
          wx.showToast({
            title: '失败',
            icon: 'error',
            duration: 3000
          });
        }
      });
    }

  },
  inputTermName: function (e) {
    let that = this;
    //  console.log(e.detail.value)
    that.setData({
      name: e.detail.value
    })

  },
  bindDateChange1: function (e) {
    let that = this;
    that.setData({
      beginweek: e.detail.value
    })
  },
  bindDateChange2: function (e) {
    let that = this;
    that.setData({
      endweek: e.detail.value
    })
  },
  longPress: function (e) {
    let that = this;
    console.log("long tap")
    wx.showModal({
      title: '删除该学期',
      content: '将该学期删除',
      confirmText: "删除",
      cancelText: "取消",
      success: function (res) {
        console.log(res);
        if (res.confirm) {
          console.log('用户点击主操作')
          //删除该课程
          wx.request({
            url: app.globalData.url + '/deleteTerm',
            method: 'DELETE',
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            data: {
              id: e.currentTarget.dataset.termid
            },
            success: res => {
              wx.showToast({
                title: '删除成功',
                icon: 'success',
                duration: 3000
              });
              that.getTermList();
            },
            fail: err => {
              wx.showToast({
                title: '失败',
                icon: 'error',
                duration: 3000
              });
            }
          })
        } else {
          console.log('用户点击辅助操作')
        }
      }
    });
  },

  getTermList: function () {
    let that = this;
    wx.request({
      url: app.globalData.url + '/findTerm',
      method: 'GET',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      data: {
        userId: app.globalData.userInfo.id
      },
      success: res => {
        console.log("获取用户的学期列表")
        console.log(res)
        if (res.data.length > 0) {
          let list = [];
          for (var i = 0; i < res.data.length; i++) {
            list.push({
              id: res.data[i].id,
              name: res.data[i].name,
              beginweek: res.data[i].beginweek.substring(0, 10),
              endweek: res.data[i].endweek.substring(0, 10)
            })
          }
          that.setData({
            termList: list
          })
        }
      }
    });
  }
})